package com.yuzhihao.writejvm.attributes.runtimevta;

import com.yuzhihao.writejvm.attributes.runtimeva.ElementValuePairs;
import com.yuzhihao.writejvm.clazz.ClassInfo;
import com.yuzhihao.writejvm.format.TargetTypeEnum;
import com.yuzhihao.writejvm.read.Parse;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Each entry in the annotations table represents a single run-time visible annotation on a type used in a declaration or expression
 *
 * @author yzh
 * @since 2021年12月11日16:00:56
 */
@Data
@Log4j2
public class TypeAnnotation implements Parse {

    public final static int target_type_u1 = 1;

    protected int target_type;

    protected TypePath tp = new TypePath();

    protected int type_index_u2 = 2;

    protected int type_index;

    protected int num_element_value_pairs_u2 = 2;

    protected int num_element_value_pairs;

    protected List<ElementValuePairs> evps = new ArrayList<>();

    @Override
    public void parse(DataInputStream dis, ClassInfo ci) {
        try {
            tp.parse(dis, ci);
            this.type_index = Parse.byteToInt(this.type_index_u2,dis);
            this.num_element_value_pairs = Parse.byteToInt(this.num_element_value_pairs_u2,dis);

            int i = 0;
            while (i < this.num_element_value_pairs){
                ElementValuePairs evp = new ElementValuePairs();
                evp.parse(dis, ci);
                this.evps.add(evp);
                i++;
            }

        } catch (IOException e) {
            log.error(e);
        }
    }

}
